import React from 'react';
import { Card, Form, Input, InputNumber, Tree, Button, message, Spin, Table, Select, DatePicker, Checkbox, Divider, Row, Col } from 'antd';
import { connect } from 'dva';
import moment from 'moment';
import { getTenantPlatformSetting } from '@/services/platformManagement';
import { modelListTenant, updateModelListTenant } from '@/services/languageModel';
import { getEmbeddingModelList, getAgentModelList } from '@/services/tenantManagement';
import styles from './index.less';
const { TreeNode } = Tree;
const moduleNameList = ["robot", 'moduleKefu', 'moduleTicket', 'largeLanguage'];
const qaNameList = ["qaKnowledge", 'atlasKnowledge', 'dialogKnowledge', 'docKnowledge'];

class TenantAddOrEdit extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      accountData: [],
      visible: false,
      id: null,

      // menuTree: [],
      // checkedKeys: [],
      menuTree: {
        artificialMenu: [],
        knowledgeMenu: [],
        llmMenu: [],
        modelMenu: [],
        robotMenu: [],
        securityMenu: [],
        staffMenu: [],
        workOrderMenu:[]
      },
      checkedKeys: {
        artificialKeys: [],
        knowledgeKeys: [],
        llmKeys: [],
        modelKeys: [],
        robotKeys: [],
        securityKeys: [],
        staffKeys: [],
        workOrderKeys:[]
      },
      expandedKeys: {
        artificialKeys: [],
        knowledgeKeys: [],
        llmKeys: [],
        modelKeys: [],
        robotKeys: [],
        securityKeys: [],
        staffKeys: [],
        workOrderKeys:[]
      },

      loading: false,
      allKfCenterChildrenIds: [],

      modelList: [],
      activeModelIds: [],
      activeModelList: [],
      indeterminateModel: false,
      checkModelAll: false,

      embeddingModelList: [],
      activeEmbeddingModelIds: [],
      activeEmbeddingModelList: [],
      indeterminateEmbedding: false,
      checkEmbeddingAll: false,

      agentModelList: [],
      activeAgentModelIds: [],
      activeAgentModelList: [],
      indeterminateAgent: false,
      checkAgentAll: false,
    }
  }

  componentDidMount() {
    const { location: { query: { tanantId } } } = this.props;
    this.initData(tanantId);
  }

  initData = (id) => {
    if (id) {
      this.getMenus(id);
      this.getModelListByTenant(id);
    } else {
      this.getMenusByAdmin();
      this.getModelList();
      this.embeddingModelList();
      this.agentModelList();
    };
    this.setState({ visible: true, id, loading: id ? true : false, })
  }

  getMenusByAdmin = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'tenantManagement/getMenuTreeByAdmin',
      payload: { id: 0 },
      callback: res => {
        console.log(res);
        this.getExpandedMenu(res);
        this.setState({ menuTree: res ? res : [] }, () => {
          this.getSelectMenu(res ? res.artificialMenu : [], "artificialKeys");
          this.getSelectMenu(res ? res.knowledgeMenu : [], "knowledgeKeys");
          this.getSelectMenu(res ? res.llmMenu : [], "llmKeys");
          this.getSelectMenu(res ? res.modelMenu : [], "modelKeys");
          this.getSelectMenu(res ? res.robotMenu : [], "robotKeys");
          this.getSelectMenu(res ? res.securityMenu : [], "securityKeys");
          this.getSelectMenu(res ? res.staffMenu : [], "staffKeys");
          this.getSelectMenu(res ? res.workOrderMenu : [], "workOrderKeys");
        });
      },
    });
  }

  getSelectMenu = (data, type) => {
    const that = this;
    let { checkedKeys } = that.state;
    data.map(item => {
      if (item.children && item.children.length > 0) {
        const childrenChecked = item.children.filter(child => child.checked);
        if (childrenChecked.length === item.children.length) {
          checkedKeys[type].push(item.id);
        };
        that.getSelectMenu(item.children, type);
      } else {
        if (item.checked) {
          checkedKeys[type].push(item.id);
        };
      }
    });
    that.setState({ checkedKeys });
  }

  getExpandedMenu = res => {
    let { expandedKeys } = this.state;
    let treeData = (data, type) => {
      data.map(item => {
        if (item.children && item.children.length > 0) {
          expandedKeys[type].push(item.id);
          treeData(item.children, type);
        }
      })
    };
    treeData(res ? res.artificialMenu : [], "artificialKeys");
    treeData(res ? res.knowledgeMenu : [], "knowledgeKeys");
    treeData(res ? res.llmMenu : [], "llmKeys");
    treeData(res ? res.modelMenu : [], "modelKeys");
    treeData(res ? res.robotMenu : [], "robotKeys");
    treeData(res ? res.securityMenu : [], "securityKeys");
    treeData(res ? res.staffMenu : [], "staffKeys");
    treeData(res ? res.workOrderMenu : [], "workOrderKeys");
    console.log(expandedKeys);
    console.log(expandedKeys.knowledgeKeys);
    this.setState({ expandedKeys });
  }

  getMenus = id => {
    const { dispatch } = this.props;
    dispatch({
      type: 'tenantManagement/getMenuTree',
      payload: { id },
      callback: res => {
        this.getExpandedMenu(res.menuTree ? res.menuTree : []);
        this.setState({ menuTree: res.menuTree ? res.menuTree : [] }, () => {
          this.getSelectMenu(res ? res.menuTree.artificialMenu : [], "artificialKeys");
          this.getSelectMenu(res ? res.menuTree.knowledgeMenu : [], "knowledgeKeys");
          this.getSelectMenu(res ? res.menuTree.llmMenu : [], "llmKeys");
          this.getSelectMenu(res ? res.menuTree.modelMenu : [], "modelKeys");
          this.getSelectMenu(res ? res.menuTree.robotMenu : [], "robotKeys");
          this.getSelectMenu(res ? res.menuTree.securityMenu : [], "securityKeys");
          this.getSelectMenu(res ? res.menuTree.staffMenu : [], "staffKeys");
          this.getSelectMenu(res ? res.menuTree.workOrderMenu : [], "workOrderKeys");
          this.setData(res);
        })
      },
    });
  }

  setData = data => {
    this.props.form.setFieldsValue({
      name: data.name,
      username: data.username,
      reviewStatus: data.reviewStatus,
      validity: data.validity ? moment(data.validity) : undefined,
      remarks: data.remarks,
      tenantType: data.tenantType,
      robotNums: data.robotNums,
      atlasNums: data.atlasNums,
      dialogNums: data.dialogNums,
      docNums: data.docNums,
      qaNums: data.qaNums,
      formNums: data.formNums,
      userNums: data.userNums,
      regulatorsNums: data.regulatorsNums,
    });
    let embeddingModelList = data.embeddingList;
    let activeEmbeddingModelList = data.embeddingList.filter(item => item.checked);
    let activeEmbeddingModelIds = activeEmbeddingModelList.map(item => item.id);

    let agentModelList = data.agentList;
    let activeAgentModelList = data.agentList.filter(item => item.checked);
    let activeAgentModelIds = activeAgentModelList.map(item => item.id);

    this.setState({
      loading: false,
      embeddingModelList,
      activeEmbeddingModelIds,
      activeEmbeddingModelList,
      indeterminateEmbedding: activeEmbeddingModelIds.length > 0 && activeEmbeddingModelIds.length < embeddingModelList.length,
      checkEmbeddingAll: activeEmbeddingModelIds.length === embeddingModelList.length,

      agentModelList,
      activeAgentModelIds,
      activeAgentModelList,
      indeterminateAgent: activeAgentModelIds.length > 0 && activeAgentModelIds.length < agentModelList.length,
      checkAgentAll: activeAgentModelIds.length === agentModelList.length,
    });
  }

  getModelList = async () => {
    const res = await modelListTenant()
    this.setState({ modelList: res })
  }

  embeddingModelList = async () => {
    const res = await getEmbeddingModelList();
    this.setState({ embeddingModelList: res })
  }

  agentModelList = async () => {
    const res = await getAgentModelList();
    this.setState({ agentModelList: res })
  }

  getModelListByTenant = async id => {
    const res = await updateModelListTenant({ tenantId: id });
    let modelList = [], activeModelIds = [], activeModelList = [];
    if (res && res.length > 0) {
      modelList = [...res];
      activeModelList = res.filter(item => item.checked);
      activeModelIds = activeModelList.map(item => item.id);
    };
    this.setState({
      modelList,
      activeModelList,
      activeModelIds,
      indeterminateModel: activeModelIds.length > 0 && activeModelIds.length < modelList.length,
      checkModelAll: activeModelIds.length === modelList.length,
    })
  }

  onCheck = (keys, type) => {
    let { checkedKeys } = this.state;
    checkedKeys[type] = keys;
    this.setState({ checkedKeys });
  };

  onExpand = (keys, type) => {
    let { expandedKeys } = this.state;
    expandedKeys[type] = keys;
    this.setState({ expandedKeys });
  }

  onClose = () => {
    this.props.form.resetFields();
    this.setState({
      visible: false,
      accountData: [],
      id: null,
      checkedKeys: [],
      loading: true,
      platformSetting: {},
      allKfCenterChildrenIds: [],
      platformSettingId: "",
    }, () => this.backAddModal())
  }

  onSubmit = () => {
    const { accountData, id, checkedKeys, activeModelList, activeEmbeddingModelList, activeAgentModelIds } = this.state;
    this.props.form.validateFields((errors, values) => {
      if (!errors) {
        if (!checkedKeys && checkedKeys.artificialKeys.length === 0 &&
          checkedKeys.knowledgeKeys.length === 0 && checkedKeys.llmKeys.length === 0 &&
          checkedKeys.modelKeys.length === 0 && checkedKeys.robotKeys.length === 0 &&
          checkedKeys.securityKeys.length === 0 && checkedKeys.staffKeys.length === 0 && checkedKeys.workOrderKeys.length === 0
        ) {
          return message.error('请选择功能权限');
        };
        if (!(activeModelList.length > 0)) {
          message.error("请选择大模型");
          return false;
        };
        if (!(activeEmbeddingModelList.length > 0)) {
          message.error("请选择embedding模型");
          return false;
        };
        let modelError = [];
        for (const item of activeModelList) {
          if (item.dateType == undefined || item.dateType == null) {
            modelError.push(item);
          };
          if (item.dateType != 0 && (item.replyLimitTimes == undefined || item.replyLimitTimes == null)) {
            modelError.push(item);
          };
        };
        if (modelError.length > 0) {
          message.error("请检查大模型调用次数的填写");
          return false;
        };
        values.llmList = [...activeModelList];
        values.embeddingList = activeEmbeddingModelList;
        values.agentList = activeAgentModelIds;
        delete values.module;
        delete values.qaType;
        if (id) {
          this.editTenant(values);
        } else {
          this.addTenant(values);
        }
      }
    })
  }

  editTenant = data => {
    const { dispatch } = this.props;
    const { checkedKeys, id } = this.state;
    console.log(data);
    let obj = { ...data, menus: [
      ...checkedKeys.artificialKeys,
      ...checkedKeys.knowledgeKeys,
      ...checkedKeys.llmKeys,
      ...checkedKeys.modelKeys,
      ...checkedKeys.robotKeys,
      ...checkedKeys.securityKeys,
      ...checkedKeys.staffKeys,
      ...checkedKeys.workOrderKeys,
    ], id, validity: moment(data.validity).format('YYYY-MM-DD'), };
    dispatch({
      type: 'tenantManagement/editTenant',
      payload: obj,
      callback: res => {
        message.info(res.message);
        this.onClose();
      },
    });
  }

  addTenant = data => {
    const { dispatch } = this.props;
    const { checkedKeys } = this.state;
    console.log(data);
    let obj = { ...data, menus: [
      ...checkedKeys.artificialKeys,
      ...checkedKeys.knowledgeKeys,
      ...checkedKeys.llmKeys,
      ...checkedKeys.modelKeys,
      ...checkedKeys.robotKeys,
      ...checkedKeys.securityKeys,
      ...checkedKeys.staffKeys,
      ...checkedKeys.workOrderKeys,
    ], validity: moment(data.validity).format('YYYY-MM-DD'), };
    dispatch({
      type: 'tenantManagement/addTenant',
      payload: obj,
      callback: res => {
        if (res.code === 0) {
          message.info(res.message);
          this.onClose();
        }
      },
    });
  }

  backAddModal = () => {
    const { history } = this.props;
    history.push({ pathname: "/enterprise/tenantManagement", });
  };

  checkFieldForPassword(rule, value, callback) {
    const reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;
    if (rule.field === 'password') {
      if (value) {
        if (reg.test(value)) {
          if (value.length > 30 || value.length < 6) {
            callback('只可填入8-20位数字和字母组合');
          } else {
            callback();
          }
        } else {
          callback('只可填入8-20位数字和字母组合');
        }
      } else {
        callback('只可填入8-20位数字和字母组合');
      }
    }
  }

  renderTreeNode = data => {
    return data.map(item => {
      if (item.children) {
        return (
          <TreeNode title={item.name} key={item.id}>{this.renderTreeNode(item.children)}</TreeNode>
        )
      };
      return <TreeNode title={item.name} key={item.id} />
    })
  }

  getAllChildrenNodes = (list, id, arr = []) => {
    for (let i in list) {
      if (list[i].id === id) {
        arr.push(list[i]);
        if (list[i].children?.length > 0) {
          this.getChild(list[i].children, arr);
        };
      } else {
        if (list[i].children?.length > 0) {
          this.getAllChildrenNodes(list[i].children, id, arr);
        };
      }
    };
    return arr.filter(v => v.id !== id);
  }

  getChild = (list, arr) => {
    list.forEach(v => {
      arr.push(v);
      if (v.children) {
        this.getChild(v.children, arr);
      }
    })
  }

  checkField(rule, value, callback) {
    const reg = /^[0-9a-zA-Z_.@]{1,}$/g;
    if (rule.field === 'username') {
      if (value) {
        if (reg.test(value)) {
          if (value.length > 30 || value.length < 6) {
            callback('支持6-30位大小写字母、数字、下划线、小数点、和@符号');
          } else {
            callback();
          }
        } else {
          callback('支持6-30位大小写字母、数字、下划线、小数点、和@符号');
        }
      } else {
        callback('支持6-30位大小写字母、数字、下划线、小数点、和@符号');
      }
    }
  }

  changeReplyLimitTimes = (value, index) => {
    let { modelList, activeModelList } = this.state;
    modelList[index].replyLimitTimes = value;
    activeModelList.forEach(item => {
      if (item.id === modelList[index].id) {
        item.replyLimitTimes = value;
      }
    });
    this.setState({ modelList, activeModelList });
  }

  changeDateType = (value, index) => {
    let { modelList, activeModelList } = this.state;
    modelList[index].dateType = value;
    activeModelList.forEach(item => {
      if (item.id === modelList[index].id) {
        item.dateType = value;
      }
    });
    this.setState({ modelList, activeModelList });
  }

  onModelChange = e => {
    const { modelList } = this.state;
    let list = modelList.filter(item => e.indexOf(item.id) > -1);
    let activeModelList = list.map(item => ({ id: item.id, dateType: item.dateType, replyLimitTimes: item.replyLimitTimes }))
    console.log(e);
    this.setState({
      activeModelList,
      activeModelIds: e,
      indeterminateModel: e.length > 0 && e.length < modelList.length,
      checkModelAll: e.length === modelList.length,
    });
  }

  onCheckAllModel = e => {
    let { modelList } = this.state;
    let activeModelList = modelList.map(item => ({ id: item.id, dateType: item.dateType, replyLimitTimes: item.replyLimitTimes }))
    this.setState({
      activeModelList,
      activeModelIds: e.target.checked ? modelList.map(item => item.id) : [],
      indeterminateModel: false,
      checkModelAll: e.target.checked,
    });
  }

  onEmbeddingChange = e => {
    const { embeddingModelList } = this.state;
    let list = embeddingModelList.filter(item => e.indexOf(item.id) > -1);
    let activeEmbeddingModelList = list.map(item => ({ id: item.id }))
    console.log(e);
    this.setState({
      activeEmbeddingModelList,
      activeEmbeddingModelIds: e,
      indeterminateEmbedding: e.length > 0 && e.length < embeddingModelList.length,
      checkEmbeddingAll: e.length === embeddingModelList.length,
    });
  }

  onCheckAllEmbedding = e => {
    let { embeddingModelList } = this.state;
    let activeEmbeddingModelList = embeddingModelList.map(item => ({ id: item.id }));
    this.setState({
      activeEmbeddingModelList,
      activeEmbeddingModelIds: e.target.checked ? embeddingModelList.map(item => item.id) : [],
      indeterminateEmbedding: false,
      checkEmbeddingAll: e.target.checked,
    });
  }

  onAgentChange = e => {
    const { agentModelList } = this.state;
    let list = agentModelList.filter(item => e.indexOf(item.id) > -1);
    let activeAgentModelList = list.map(item => ({ id: item.id }))
    console.log(e);
    this.setState({
      activeAgentModelList,
      activeAgentModelIds: e,
      indeterminateAgent: e.length > 0 && e.length < agentModelList.length,
      checkAgentAll: e.length === agentModelList.length,
    });
  }

  onCheckAllAgent = e => {
    let { agentModelList } = this.state;
    let activeAgentModelList = agentModelList.map(item => ({ id: item.id }));
    this.setState({
      activeAgentModelList,
      activeAgentModelIds: e.target.checked ? agentModelList.map(item => item.id) : [],
      indeterminateAgent: false,
      checkAgentAll: e.target.checked,
    });
  }

  render() {
    const { getFieldDecorator, getFieldValue } = this.props.form;
    const { menuTree, id, checkedKeys, expandedKeys, loading, modelList, embeddingModelList, agentModelList, activeModelIds, activeEmbeddingModelIds, activeAgentModelIds } = this.state;

    return (
      <Card bordered={false}>
        <Spin spinning={loading}>
          <Form layout="inline" style={{ marginBottom: 45 }}>
            <div>
              <div className={styles.formTitle}>基础信息</div>
              <div className={styles.formCard}>
                <Form.Item label={'企业名称'}>
                  {getFieldDecorator('name', {
                    rules: [{ required: true, message: '企业名称必填' }],
                    getValueFromEvent: event => event.target.value.replace(/\s+/g, '')
                  })(<Input placeholder={'请填写企业名称'} maxLength={60} style={{ width: '200px' }} autoComplete="off" />)}
                </Form.Item>
                <Form.Item label={'账号'}>
                  {getFieldDecorator('username', {
                    rules: [{ required: true, message: '账号必填' }, { validator: this.checkField },]
                  })(<Input placeholder={'请填写账号'} maxLength={60} style={{ width: '200px' }} autoComplete="off" disabled={id ? true : false} />)}
                </Form.Item>
                {id ? '' : (
                  <Form.Item label={'密码'}>
                    {getFieldDecorator('password', {
                      rules: [{ required: true, message: '密码必填' }, { validator: this.checkFieldForPassword, }]
                    })(<Input.Password placeholder={'请填写密码'} maxLength={60} style={{ width: '200px' }} autoComplete="new-password" />)}
                  </Form.Item>
                )}
              </div>
            </div>
            <Divider />
            <div>
              <div className={styles.formTitle}>授权时间</div>
              <div className={styles.formCard}>
                <Form.Item label="有效期">
                  {getFieldDecorator('validity', {
                    rules: [{ required: true, message: '请选择有效期' }],
                  })(<DatePicker style={{ width: '200px' }} />)}
                </Form.Item>
                <Form.Item label={'备注'}>
                  {getFieldDecorator('remarks')(<Input placeholder="请输入" style={{ width: '200px' }} />)}
                </Form.Item>
              </div>
            </div>
            <Divider />
            <div>
              <div className={styles.formTitle}>功能模块</div>
              <div className={styles.formCard}>
                {/* {getFieldDecorator('menus')(
                    <Tree checkable checkedKeys={checkedKeys} onCheck={this.onCheck} style={{ maxHeight: '300px', overflowY: 'auto' }}>
                      {this.renderTreeNode(menuTree)}
                    </Tree>
                  )} */}
                <div className={styles.formMenuTree}>
                  <span>
                    <span>交互体验</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "modelKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.modelKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.modelKeys}
                      onCheck={e => this.onCheck(e, "modelKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.modelMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>知识中心</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "knowledgeKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.knowledgeKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.knowledgeKeys}
                      onCheck={e => this.onCheck(e, "knowledgeKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.knowledgeMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>能力中心</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "llmKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.llmKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.llmKeys}
                      onCheck={e => this.onCheck(e, "llmKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.llmMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>数据中心</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "artificialKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.artificialKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.artificialKeys}
                      onCheck={e => this.onCheck(e, "artificialKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.artificialMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>企业管理</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "robotKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.robotKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.robotKeys}
                      onCheck={e => this.onCheck(e, "robotKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.robotMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>安全中心</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "securityKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.securityKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.securityKeys}
                      onCheck={e => this.onCheck(e, "securityKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.securityMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>用户管理</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "staffKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.staffKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.staffKeys}
                      onCheck={e => this.onCheck(e, "staffKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.staffMenu)}
                    </Tree>
                  </span>
                  <span>
                    <span>工单中心</span>
                    <Tree
                      checkable
                      onExpand={e => this.onExpand(e, "workOrderKeys")}
                      expandedKeys={JSON.parse(JSON.stringify(expandedKeys.workOrderKeys))}
                      autoExpandParent={true}
                      checkedKeys={checkedKeys.workOrderKeys}
                      onCheck={e => this.onCheck(e, "workOrderKeys")}
                      style={{ width: '300px', height: '300px', overflowX: 'hidden', overflowY: 'auto' }}
                    >
                      {this.renderTreeNode(menuTree.workOrderMenu)}
                    </Tree>
                  </span>
                </div>
              </div>
            </div>
            <Divider />
            <div>
              <div className={styles.formTitle}>资源限制</div>
              <div className={styles.formCard} style={{ display: 'flex', flexWrap: 'wrap', gap: '16px' }}>
                <span>
                  <span className={styles.formCardLabel}>应用</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('robotNums', {
                      initialValue: 5,
                      rules: [{ required: true, message: '应用数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个应用</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>知识点</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('qaNums', {
                      initialValue: 1000,
                      rules: [{ required: true, message: '知识点数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个知识点</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>多轮</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('dialogNums', {
                      initialValue: 50,
                      rules: [{ required: true, message: '多轮数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个多轮</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>导图</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('atlasNums', {
                      initialValue: 50,
                      rules: [{ required: true, message: '导图数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个导图</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>文档</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('docNums', {
                      initialValue: 50,
                      rules: [{ required: true, message: '文档数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个文档</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>表单</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('formNums', {
                      initialValue: 50,
                      rules: [{ required: true, message: '文档数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个表单</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>用户</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('userNums', {
                      initialValue: 50,
                      rules: [{ required: true, message: '文档数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个用户</span>
                  </span>
                </span>
                <span>
                  <span className={styles.formCardLabel}>管理员</span>
                  <span className={styles.formCardLi}>
                    <span style={{ marginRight: 5 }}>最大可以创建</span>
                    {getFieldDecorator('regulatorsNums', {
                      initialValue: 50,
                      rules: [{ required: true, message: '文档数量必填' }],
                    })(<InputNumber style={{ width: '100px' }} min={1} precision={0} />)}
                    <span style={{ marginLeft: 5 }}>个管理员</span>
                  </span>
                </span>
              </div>
            </div>
            <Divider />
            <div>
              <div className={styles.formTitle}>分配大模型</div>
              <div className={styles.formCard}>
                <Checkbox
                  indeterminate={this.state.indeterminateModel}
                  onChange={this.onCheckAllModel}
                  checked={this.state.checkModelAll}
                  style={{ padding: '2px 16px', marginLeft: 6, height: '36px' }}
                >
                  全部
                </Checkbox>
                <Checkbox.Group style={{ width: '100%' }} value={activeModelIds} onChange={this.onModelChange}>
                  <Row gutter={[16, 16]}>
                    {modelList.map((item, index) => {
                      return (
                        <Col style={{ width: '500px' }} key={index}>
                          <span className={styles.formCardLi} style={{ width: '100%' }}>
                            <Checkbox value={item.id} style={{ marginRight: 5 }}>{item.name}</Checkbox>
                            <Select value={item.dateType} onChange={(value) => this.changeDateType(value, index)} style={{ width: 100, marginRight: 5 }}>
                              <Select.Option value={0}>无限制</Select.Option>
                              <Select.Option value={1}>每天</Select.Option>
                              <Select.Option value={2}>每月</Select.Option>
                              <Select.Option value={3}>每年</Select.Option>
                            </Select>
                            {item.dateType == 0 ? <span>调用</span> : (
                              <span>
                                <span style={{ marginRight: 5 }}>最大可以调用</span>
                                <InputNumber style={{ width: '100px' }} min={0} precision={0} value={item.replyLimitTimes} onChange={(value) => this.changeReplyLimitTimes(value, index)} />
                                <span style={{ marginLeft: 5 }}>次</span>
                              </span>
                            )}
                          </span>
                        </Col>
                      )
                    })}
                  </Row>
                </Checkbox.Group>
              </div>
            </div>
            <Divider />
            <div>
              <div className={styles.formTitle}>分配搜索模型</div>
              <div className={styles.formCard}>
                <Checkbox
                  indeterminate={this.state.indeterminateEmbedding}
                  onChange={this.onCheckAllEmbedding}
                  checked={this.state.checkEmbeddingAll}
                  style={{ marginLeft: 6, height: '36px' }}
                >
                全部
                </Checkbox>
                <Checkbox.Group style={{ width: '100%' }} value={activeEmbeddingModelIds} onChange={this.onEmbeddingChange}>
                  <Row style={{ display: 'flex', flexWrap: 'wrap' }} className={styles.formCardRowList}>
                    {embeddingModelList.map((item, index) => {
                      return <Col key={index}> <Checkbox value={item.id}>{item.name}</Checkbox> </Col>
                    })}
                  </Row>
                </Checkbox.Group>
              </div>
            </div>
            <Divider />
            <div>
              <div className={styles.formTitle}>分配助手</div>
              <div className={styles.formCard}>
                <Checkbox
                  indeterminate={this.state.indeterminateAgent}
                  onChange={this.onCheckAllAgent}
                  checked={this.state.checkAgentAll}
                  style={{ marginLeft: 6, height: '36px' }}
                >
                全部
                </Checkbox>
                <Checkbox.Group style={{ width: '100%' }} value={activeAgentModelIds} onChange={this.onAgentChange}>
                  <Row style={{ display: 'flex', flexWrap: 'wrap' }} className={styles.formCardRowList}>
                    {agentModelList.map((item, index) => {
                      return <Col key={index}> <Checkbox value={item.id}>{item.name}</Checkbox> </Col>
                    })}
                  </Row>
                </Checkbox.Group>
              </div>
            </div>
          </Form>
        </Spin>
        <div
          style={{
            position: 'absolute',
            right: 0,
            bottom: 0,
            width: '100%',
            borderTop: '1px solid #e9e9e9',
            padding: '10px 16px',
            background: '#fff',
          }}
        >
          <Button onClick={this.onClose} style={{ marginRight: 8 }}>取消</Button>
          <Button onClick={this.onSubmit} type="primary">确定</Button>
        </div>
      </Card>
    )
  }
}

export default connect(({ user }) => ({
  currentUser: user.currentUser,
}))(Form.create()(TenantAddOrEdit));
